﻿package cn.poco.pepsi.adapter;

import java.util.List;

import cn.poco.pepsi.R;
import cn.poco.pepsi.bean.Circle;
import cn.poco.pepsi.bean.Dish;
import cn.poco.pepsi.bean.HotSearchKey;



import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 适配搜索主界面的三个列表
 * 
 * @author tanglx
 * 
 */
public class SearchMainListAdapt extends BaseAdapter {
	private Context context;
	private List<?> list;
	private int type;
	private boolean flag=true;

	class CustomLayout {
		TextView textView;
		ImageView pepsiV;
	}

	//type 哪种方式  对应四个列表
	public SearchMainListAdapt(Context context, List<?> list, int type) {
		this.context = context;
		this.list = list;
		this.type = type;
	}

	@Override
	public int getCount() {
		int returnInt = 0;
		switch (type) {
		case 1:
			if (list.size() > 5) {
				returnInt = 5;
			} else {
				returnInt = list.size();
			}
			break;
		case 2:
			if (list.size() > 5) {
				returnInt = 5;
			} else {
				returnInt = list.size();
			}
			break;
		case 3:
			returnInt = 4;
			break;
		case 4:
			if (list.size() > 5) {
				returnInt = 5;
			} else {
				returnInt = list.size();
			}
			break;
			
		default:
			break;
		}
		return returnInt;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		CustomLayout cLayout,cLayout2;
		if (convertView == null) {
			cLayout = new CustomLayout();
			convertView = LayoutInflater.from(context).inflate(R.layout.search_main_item, null);
			cLayout.textView = (TextView) convertView.findViewById(R.id.search_main_item_textview);
			
			convertView.setTag(cLayout);
		} else {
			cLayout = (CustomLayout) convertView.getTag();
		}
		
		switch (type) {
		case 1:
			cLayout.textView.setText(((Circle) list.get(position)).getName());
			cLayout.textView.setTextColor(Color.parseColor("#003D79"));
			break;
		case 2:
			cLayout.textView.setText(((Dish) list.get(position)).getName());
			cLayout.textView.setTextColor(Color.parseColor("#003D79"));
			break;
		case 3:
			//
			cLayout2 = new CustomLayout();
			convertView = LayoutInflater.from(context).inflate(R.layout.search_main_item_addpepsi_img, null);
			cLayout2.textView = (TextView) convertView.findViewById(R.id.search_main_item_textview);
			cLayout2.pepsiV=(ImageView) convertView.findViewById(R.id.pepsilog_view);
			if(flag){
				cLayout2.pepsiV.setVisibility(View.VISIBLE);
				flag=false;
			}else{
				cLayout2.pepsiV.setVisibility(View.GONE);
			}
			
			convertView.setTag(cLayout2);
			
			cLayout2.textView.setText(list.get(position).toString());
			cLayout2.textView.setTextColor(Color.parseColor("#003D79"));
			cLayout2.pepsiV.setImageResource(R.drawable.pepsi_log);
		
			break;
		case 4:
			cLayout.textView.setText(((HotSearchKey) list.get(position)).getKeyword());
			cLayout.textView.setTextColor(Color.parseColor("#003D79"));
			break;
		default:
			break;
		}
		return convertView;
	}

}
